package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.EasyUIResult;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.service.TbContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TbContentServiceImpl implements TbContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    /**
     * 查询content
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public EasyUIResult getTbContentByCategoryIdList(Long categoryId, int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<TbContent> tbContentList = tbContentMapper.selectByExampleWithBLOBs(tbContentExample);

        PageInfo<TbContent> pageInfo = new PageInfo<>(tbContentList);
        EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(), tbContentList);
        return easyUIResult;
    }

    /**
     * 新增content数据
     * @param tbContent
     * @return
     */
    @Override
    public TaotaoResult saveTbContent(TbContent tbContent) {
        Date time = new Date();
        tbContent.setCreated(time);
        tbContent.setUpdated(time);
        tbContentMapper.insertSelective(tbContent);
        return TaotaoResult.ok();
    }

    /**
     * 删除content数据
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public TaotaoResult deleteTbContent(Long[] ids) {
        for (Long id : ids){
            tbContentMapper.deleteByPrimaryKey(id);
        }
        return TaotaoResult.ok();
    }

    /**
     * 更新content数据
     * @param tbContent
     * @return
     */
    @Override
    public TaotaoResult updateTbContent(TbContent tbContent) {
        Date time = new Date();
        tbContent.setUpdated(time);
        tbContentMapper.updateByPrimaryKeyWithBLOBs(tbContent);
        return TaotaoResult.ok();
    }
}
